<template>
  <div class="app-container">
    <!-- 按仓库类型仓库商品 -->
    <FloatMenu />
    <el-form
      ref="minusForm"
      :inline="true"
      :model="dataJson.searchForm"
      label-position="getLabelPosition()"
    >
      <el-form-item>
        <select-dict
          v-model="dataJson.searchForm.type"
          :para="CONSTANTS.DICT_M_WAREHOUSE_TYPE"
          init-placeholder="仓库类型"
          :disabled="isViewModel"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="dataJson.searchForm.warehouse_ids"
          placeholder="请选择仓库"
          filterable
          :loading="settings.loading"
          loading-text="拼命加载..."
          multiple
          collapse-tags
          clearable
          style="width:200px"
        >
          <el-option
            v-for="item in dataJson.warehouseComboList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model.trim="dataJson.searchForm.goods_code"
          clearable
          placeholder="请输入商品编码"
          @keyup.enter.native="handleSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model.trim="dataJson.searchForm.goods_name"
          clearable
          placeholder="请输入商品名称"
          @keyup.enter.native="handleSearch"
        />
      </el-form-item>
      <el-form-item>
        <select-dict
          v-model="dataJson.searchForm.batch"
          :para="CONSTANTS.DICT_BUSINESS_START_TIME"
          init-placeholder="业务启动日期"
        />
      </el-form-item>
      <el-form-item style="float:right">
        <el-button
          v-popover:popover
          type="primary"
          plain
          icon="perfect-icon-reset"
          @click="doResetSearch"
        >重置</el-button>
      </el-form-item>
      <el-form-item style="float:right">
        <el-button
          type="primary"
          plain
          :loading="settings.loading"
          icon="el-icon-search"
          @click="handleSearch"
        >查询</el-button>
      </el-form-item>
    </el-form>
    <el-button-group>

      <el-button
        v-permission="'P_ALL_WAREHOUS:EXPORT'"
        :disabled="!settings.btnShowStatus.showExport"
        type="primary"
        icon="el-icon-s-management"
        :loading="settings.loading"
        @click="handleExport"
      >导出</el-button>

    </el-button-group>

    <div class="div-sum">
      <div class="right">
        <span class="count-title">合计入库库存：</span><span class="count-data">{{ dataJson.sumData.in_qty === null?'0': isZhongLin ? formatNumber3(dataJson.sumData.in_qty) : formatNumberTwo(dataJson.sumData.in_qty,true,2) }}</span>
        <span class="count-title">合计退货库存：</span><span class="count-data">{{ dataJson.sumData.return_sum_qty == null?'0': isZhongLin ? formatNumber3(dataJson.sumData.return_sum_qty) : formatNumberTwo(dataJson.sumData.return_sum_qty,true,2) }}</span>
        <span class="count-title">合计出库库存：</span><span class="count-data">{{ dataJson.sumData.out_qty === null?'0': isZhongLin ? formatNumber3(dataJson.sumData.out_qty) : formatNumberTwo(dataJson.sumData.out_qty,true,2) }}</span>
        <span class="count-title">合计调整库存：</span><span class="count-data">{{ dataJson.sumData.qty_diff === null?'0': isZhongLin ? formatNumber3(dataJson.sumData.qty_diff) : formatNumberTwo(dataJson.sumData.qty_diff,true,2) }}</span>
        <span class="count-title">存货合计：</span><span class="count-data">{{ dataJson.sumData.qty === null? '0': isZhongLin ? formatNumber3(dataJson.sumData.qty) : formatNumberTwo(dataJson.sumData.qty,true,2) }}</span>
      </div>
    </div>

    <el-table
      ref="multipleTable"
      v-loading="settings.loading"
      :data="dataJson.listData"
      :element-loading-text="'正在拼命加载中...'"
      element-loading-background="rgba(255, 255, 255, 0.5)"
      :height="settings.tableHeight"
      columns_index_key="true"
      stripe
      border
      fit
      highlight-current-row
      :default-sort="{prop: 'warehouse_type_name', order: 'ascending'}"
      style="width: 100%"
      @row-click="handleRowClick"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="45"
        prop="id"
      />
      <el-table-column
        type="index"
        width="45"
        prop="No"
        label="No"
      />
      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        :auto-fit="true"
        min-width="160"
        align="left"
        prop="warehouse_type_name"
        label="仓库类型"
      />
      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        :auto-fit="true"
        min-width="160"
        align="left"
        prop="warehouse_name"
        label="仓库"
      />
      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        :auto-fit="true"
        min-width="160"
        align="left"
        prop="goods_code"
        label="物料编码"
      />
      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        :auto-fit="true"
        min-width="160"
        align="left"
        prop="goods_name"
        label="物料名称"
      />
      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        :auto-fit="true"
        min-width="160"
        align="right"
        prop="in_qty"
        label="合计入库库存数量"
      >
        <template v-slot="scope">
          <router-link :to="{name:constants_program.P_IN,query:{goods_name:scope.row.goods_code, warehouse_id:scope.row.warehouse_id, batch: dataJson.searchForm.batch, fullpath:true}}"><a href="javascript:void(0)">{{ scope.row.in_qty === null?'': isZhongLin ? formatNumber3(scope.row.in_qty) : formatNumberTwo(scope.row.in_qty,true,2) }}</a></router-link>
        </template>
      </el-table-column>

      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        :auto-fit="true"
        align="right"
        min-width="160"
        prop="return_qty"
        label="合计退货数量"
      >
        <template v-slot="scope">
          <router-link :to="{name:constants_program.P_IN,query:{goods_name:scope.row.goods_code, warehouse_id:scope.row.warehouse_id, batch: dataJson.searchForm.batch, type:constants_dict.DICT_B_IN_PLAN_TYPE_JG_TH,fullpath:true}}"><a href="javascript:void(0)"> {{ scope.row.return_qty === null?'':isZhongLin ? formatNumber3(scope.row.return_qty) : formatNumberTwo(scope.row.return_qty,true,2) }}</a></router-link>
        </template>

      </el-table-column>

      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        :auto-fit="true"
        min-width="160"
        align="right"
        prop="out_qty"
        label="合计出库库存数量"
      >
        <template v-slot="scope">
          <router-link :to="{name:constants_program.P_OUT,query:{goods_name:scope.row.goods_code, warehouse_id:scope.row.warehouse_id, batch: dataJson.searchForm.batch, fullpath:true}}"><a href="javascript:void(0)">{{ scope.row.out_qty === null?'': isZhongLin ? formatNumber3(scope.row.out_qty) : formatNumberTwo(scope.row.out_qty,true,2) }}</a></router-link>
        </template>
      </el-table-column>
      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        :auto-fit="true"
        min-width="160"
        align="right"
        prop="qty_diff"
        label="合计调整库存数量"
      >
        <template v-slot="scope">
          <router-link :to="{name:constants_program.P_ADJUST,query:{goods_name:scope.row.goods_code, warehouse_id:scope.row.warehouse_id, batch: dataJson.searchForm.batch, fullpath:true}}"><a href="javascript:void(0)">{{ scope.row.qty_diff === null?'': isZhongLin ? formatNumber3(scope.row.qty_diff) : formatNumberTwo(scope.row.qty_diff,true,2) }}</a></router-link>
        </template>
      </el-table-column>
      <el-table-column
        sortable="custom"
        :sort-orders="settings.sortOrders"
        align="right"
        min-width="160"
        :auto-fit="true"
        prop="qty"
        label="存货合计"
      >
        <template v-slot="scope">
          {{ scope.row.qty === null?'': isZhongLin ? formatNumber3(scope.row.qty) : formatNumberTwo(scope.row.qty,true,2) }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      ref="minusPaging"
      :total="dataJson.paging.total"
      :page.sync="dataJson.paging.current"
      :limit.sync="dataJson.paging.size"
      @pagination="getDataList"
    />

  </div>
</template>

<style scoped>
::v-deep .el-tabs__item {
  height: 30px;
  line-height: 30px;
}
::v-deep .el-tabs__header {
  margin: 0 0 5px;
}
.floatRight {
  float: right;
}
.floatLeft {
  float: left;
}
.el-form-item .el-select {
  width: 100%;
}
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 371px;
}
.el-button-group {
  margin-bottom: 10px;
}
.el-input-group__append_select,
.el-input-group__append {
  color: #ffffff;
  background-color: #1890ff;
  border-color: #1890ff;
}
.el-input-group__append_reset {
  color: #ffffff;
  background-color: #f56c6c;
  border-color: #f56c6c;
}
a {
  color: #52b5f2;
}
.div-sum {
  width: 100%;
  height: 35px;
  padding: 5px 5px;
  margin: 0;
  box-sizing: border-box;
  border-radius: 4px;
  transition: opacity 0.2s;
  background-color: #fff;
  color: #666;
  font-size: 16px;
  border-top: 1px solid #dfe6ec;
  border-left: 1px solid #dfe6ec;
  border-right: 1px solid #dfe6ec;
}
.right {
  position: absolute;
  right: 10px;
  margin-right: 10px;
}
.left {
  position: absolute;
  left: 10px;
  margin-left: 10px;
}
.count-data {
  color: #f00;
  font-size: 20px;
}
.count-title {
  margin-left: 10px;
}
.el-form-item--mini.el-form-item {
  margin-bottom: 10px;
}
</style>

<script>
import { queryReportInventoryList, queryReportInventorySum } from '@/api/50_query/inventory_warehouse_goods'
import { exportApi, exportAllApi } from '@/api/50_query/inventory_warehouse'
import resizeMixin from './mixin/resizeHandlerMixin'
import Pagination from '@/components/Pagination'
import deepCopy from 'deep-copy'
import { getWarehouseComboListApi } from '@/api/30_wms/warehouse/warehouse'
import SelectDict from '@/components/00_dict/select/SelectDict'
import constants_program from '@/common/constants/constants_program'
import permission from '@/directive/permission/index.js' // 权限判断指令
import urlUtil from '@/utils/urlUtil'
import { mapState } from 'vuex'
import constants_dict from '@/common/constants/constants_dict'

export default {
  components: { Pagination, SelectDict },
  directives: { permission },
  mixins: [resizeMixin],
  props: {},
  data () {
    return {
      constants_program: constants_program,
      dataJson: {
        // 查询使用的json
        searchForm: {
          goods_name: '',
          type: '',
          goods_code: null,
          // 翻页条件
          pageCondition: {
            current: 1,
            size: 20,
            sort: 'warehouse_type_name' // 排序
          },
          // 分组类型
          query_type: 2,
          // 启动日期
          batch: ''
        },
        // 分页控件的json
        paging: deepCopy(this.PARAMETERS.PAGE_JSON),
        // table使用的json
        listData: null,
        sumData: {
          qty_diff: 0,
          in_qty: 0,
          out_qty: 0,
          qty: 0
        },
        // 单条数据 json的，初始化原始数据
        tempJsonOriginal: {
          id: undefined,
          name: '',
          value: '',
          descr: '',
          wlb_data: null
        },
        // 单条数据 json
        currentJson: null,
        tempJson: null,
        inputSettings: {
          maxLength: {
            name: 20
          }
        },
        // 当前表格中的索引，第几条
        rowIndex: 0,
        // 当前选中的行（checkbox）
        multipleSelection: []
      },
      popSettingsData: {
        dialogFormVisible: false
      },
      // 页面设置json
      settings: {
        // 表格排序规则
        sortOrders: deepCopy(this.PARAMETERS.SORT_PARA),
        // 按钮状态
        btnShowStatus: {
          showExport: false
        },
        // loading 状态
        loading: true,
        tableHeight: this.setUIheight(),
        duration: 4000
      }
    }
  },
  computed: {
    constants_dict () {
      return constants_dict
    },
    ...mapState('dictDefaultValue', ['dict_value']),
    // 是否为查看模式
    isViewModel () {
      if ((this.popSettingsData.dialogStatus === 'view') && (this.popSettingsData.dialogFormVisible === true)) {
        // 查看模式
        return true
      } else {
        // 非查看模式
        return false
      }
    },
    // 判断是否是中林环境
    isZhongLin () {
      const href = window.location.href
      if (href.indexOf('shyiyuanth') !== -1 || href.indexOf('172.21.1.249') !== -1 || href.indexOf('localhost') !== -1) {
        return true
      } else {
        return false
      }
    }
  },
  // 监听器
  watch: {
    // 选中的数据，使得导出按钮可用，否则就不可使用
    'dataJson.multipleSelection': {
      handler (newVal, oldVal) {
        if (newVal.length > 0) {
          this.settings.btnShowStatus.showExport = true
        } else {
          this.settings.btnShowStatus.showExport = false
        }
      }
    }
  },
  created () {
    // 初始化仓库下拉框
    this.getWarehouseData()
    // 初始化查询
    this.getDataList()
    // 数据初始化
    this.dataJson.tempJson = Object.assign({}, this.dataJson.tempJsonOriginal)
    if (this.$route.query.type !== null && this.$route.query.type !== undefined) {
      this.dataJson.searchForm.type = this.$route.query.type
    }
    if (this.$route.query.code !== null && this.$route.query.code !== undefined) {
      this.dataJson.searchForm.goods_code = this.$route.query.code
    }
    if (this.$route.query.goods_name !== null && this.$route.query.goods_name !== undefined) {
      this.dataJson.searchForm.goods_name = this.$route.query.goods_name
    }
    if (this.$route.query.batch !== null && this.$route.query.batch !== undefined) {
      this.dataJson.searchForm.batch = this.$route.query.batch
    } else {
      this.dataJson.searchForm.batch = this.dict_value
    }
    location.href = urlUtil.delParam(window.location.href, 'fullpath')
  },
  mounted () {
    // 描绘完成

  },
  methods: {
    // 获取行索引
    getRowIndex (row) {
      const _index = this.dataJson.listData.lastIndexOf(row)
      return _index
    },
    // 行点击
    handleRowClick (row) {
      this.dataJson.rowIndex = this.getRowIndex(row)
    },
    handleSearch () {
      // 查询
      this.dataJson.searchForm.pageCondition.current = 1
      this.dataJson.paging.current = 1
      this.getDataList()
      // 清空选择
      this.dataJson.multipleSelection = []
      this.$refs.multipleTable.clearSelection()
    },
    handleSortChange (column) {
      // 服务器端排序
      if (column.order === 'ascending') {
        this.dataJson.searchForm.pageCondition.sort = column.prop
      } else if (column.order === 'descending') {
        this.dataJson.searchForm.pageCondition.sort = '-' + column.prop
      }
      this.getDataList()
    },
    getDataList () {
      this.dataJson.searchForm.pageCondition.current = this.dataJson.paging.current
      this.dataJson.searchForm.pageCondition.size = this.dataJson.paging.size
      // 查询逻辑
      this.settings.loading = true
      queryReportInventoryList(this.dataJson.searchForm).then(response => {
        // 增加对象属性，columnTypeShowIcon，columnNameShowIcon
        this.dataJson.listData = response.data.records
        this.dataJson.paging = response.data
        this.dataJson.paging.records = {}
      }).finally(() => {
        this.settings.loading = false
      })
      // 查询合计信息
      queryReportInventorySum(this.dataJson.searchForm).then(response => {
        if (response.data !== null) {
          this.dataJson.sumData = response.data
        }
      }).finally(() => {
      })
    },
    // 重置查询区域
    doResetSearch () {
      this.dataJson.searchForm = this.$options.data.call(this).dataJson.searchForm
    },

    // 获取row-key
    getRowKeys (row) {
      return row.id
    },
    // table选择框
    handleSelectionChange (val) {
      this.dataJson.multipleSelection = val
    },
    getWarehouseData () {
      getWarehouseComboListApi({ enable: true }).then((_data) => {
        this.dataJson.warehouseComboList = _data.data
        this.settings.loading = false
      }, (_error) => {
      })
    },

    // 导出按钮
    handleExport () {
      // 没有选择任何数据的情况
      if (this.dataJson.multipleSelection.length <= 0) {
        this.$alert('请在表格中选择数据进行导出', '未选择数据错误', {
          confirmButtonText: '关闭',
          type: 'error'
        }).then(() => {
          this.settings.btnStatus.showExport = false
        })
      } else if (this.dataJson.multipleSelection.length === this.dataJson.listData.length) {
        // 选择全部的时候
        this.$confirm('请选择：当前页数据导出，全数据导出？', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '全数据导出',
          cancelButtonText: '当前页数据导出'
        }).then(() => {
          this.handleExportAllData()
        }).catch(action => {
          // 右上角X
          if (action !== 'close') {
            // 当前页所选择的数据导出
            this.handleExportSelectionData()
          }
        })
      } else {
        // 部分数据导出
        this.handleExportSelectionData()
      }
    },
    // 全部数据导出
    handleExportAllData () {
      // loading
      this.settings.loading = true

      // 开始导出
      exportAllApi(this.dataJson.searchForm).then(response => {
        this.settings.loading = false
      }).finally(() => {
        this.settings.loading = false
      })
    },
    // 部分数据导出
    handleExportSelectionData () {
      // loading
      this.settings.loading = true
      const selectionJson = []
      this.dataJson.multipleSelection.forEach(function (value, index, map) {
        selectionJson.push({ 'id': value.id })
      })
      // 开始导出
      exportApi(selectionJson).then(response => {
        this.settings.loading = false
      }).finally(() => {
        this.settings.loading = false
      })
    }

  }
}
</script>
